package cn.hesion.leetcode;

import java.util.Stack;

public class RemoveDuplicates {

    public String removeDuplicates(String s) {
        if (s.length() == 0) {
            return s;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (stack.empty()) {
                stack.push(c);
            } else {
                Character topC = stack.peek();
                if (c == topC) {
                    stack.pop();
                } else {
                    stack.push(c);
                }
            }
        }
        if (stack.size() > 0) {
            StringBuilder finnalStr = new StringBuilder();
            int size = stack.size();
            while (size >= 1){
                finnalStr.append(stack.pop());
                size--;
            }
            return finnalStr.reverse().toString();
        }else {
            return "";
        }
    }


    public static void main(String[] args) {
        RemoveDuplicates instance = new RemoveDuplicates();
        System.out.println(instance.removeDuplicates("abbbabaaa"));
    }
}
